package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.DailyNewsComment;
import cn.wolfcode.trip.base.query.DailyNewsCommentQuery;
import cn.wolfcode.trip.base.service.IDailyNewsCommentService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/dailyNewsComment")
public class DailyNewsConmmentController {
    @Autowired
    private IDailyNewsCommentService dailyNewsCommentService;

    /**
     * 获取评论的总数
     */
    @GetMapping("/{news_id}")
    public Integer countComment( @PathVariable Long news_id){
        Integer count = dailyNewsCommentService.selectByNewsId(news_id);
        return count;
    }

    /**
     * 渲染评论详情的方法
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo selectForComment(DailyNewsCommentQuery qo){
        qo.setOrderBy("releaseTime desc");
        PageInfo<DailyNewsComment> dailyNewsCommentPageInfo = dailyNewsCommentService.selectForComment(qo);
        return dailyNewsCommentPageInfo;
    }

    /**
     * 添加评论的功能
     */
    //@PostMapping("/{user.id}/{news.id}")
    @PostMapping
    public JSONResult saveComment(DailyNewsComment dailyNewsComment){
        dailyNewsComment.setReleaseTime(new Date());
        dailyNewsCommentService.insert(dailyNewsComment);
        return new JSONResult();
    }
    //获取该用户的所有的评论
    @GetMapping("/{userId}")
    public PageInfo listDailyNews(DailyNewsCommentQuery qo){
        qo.setOrderBy("releaseTime desc");
        return dailyNewsCommentService.selectByUserId(qo);
    }

}
